package com.yunfangdata.web.controller;

import com.yunfangdata.domain.Test;
import com.yunfangdata.query.TestQueryObject;
import com.yunfangdata.service.ITestService;
import com.yunfangdata.util.JsonResult;
import com.yunfangdata.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("test")
public class TestController {
    @Autowired
    private ITestService testService;


    @RequestMapping("")
    public String index(){
        return "test/list";
    }
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(TestQueryObject qo){
        return testService.query(qo);
    }

  @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Test entity) {
        JsonResult result = new JsonResult();
        try{

            testService.save(entity);

        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("保存操作失败");
        }
        return  result;
    }

    //删除操作
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try{
            testService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("删除失败");
        }
        return  result;
    }






    @RequestMapping("update")
    @ResponseBody
    public JsonResult update(Test entity) {
        JsonResult result = new JsonResult();
        try{
            testService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result = new JsonResult("更新操作失败");
        }
        return  result;
    }

    @RequestMapping("changeState")
    @ResponseBody
    public  JsonResult changeSate(Long id){
        JsonResult result = new JsonResult();
        try {
            testService.changeState(id);
        }catch (Exception e){
            e.printStackTrace();
            result=new JsonResult("修改错误");
        }
        return result;

    }





}